package me.ymfs.springboot.guice.exception;

import lombok.Data;

@Data
public class BaseException extends RuntimeException {

    private final ErrorCode errorCode;

    public BaseException(ErrorCodeSupplier errorCode, String message) {
        this(errorCode, message, null);
    }

    public BaseException(ErrorCodeSupplier errorCode, Throwable cause) {
        this(errorCode, null, cause);
    }

    public BaseException(ErrorCodeSupplier errorCode, String message, Throwable cause) {
        super(message, cause);
        this.errorCode = errorCode.toErrorCode();
    }

    public BaseException(ErrorCodeSupplier errorCode, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.errorCode = errorCode.toErrorCode();
    }

    @Override
    public String getMessage() {
        String message = super.getMessage();
        if (message == null && this.getErrorCode() != null) {
            message = this.getCause().getMessage();
        }

        if (message == null) {
            message = this.errorCode.getMsg();
        }
        return message;
    }
}
